package com.suyx.job.utils;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.baidu.aip.ocr.AipOcr;


public class SampleOCRUtil {
    //设置APPID/AK/SK
    public static final String APP_ID = "16353031";
    public static final String API_KEY = "GlHbe8dzxGBneyjwSQh4pDAC";
    public static final String SECRET_KEY = "VCebnzaPyurv9mG665T3jT9GyElvoMci";

    public static void main(String[] args) throws Exception {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        //System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
        String addrImg = "d:\\android\\imges\\test3.png";
        String result = SampleOCRUtil.sampleBasic(addrImg);
        System.out.println(result);
        
    }
    
    public static String sampleAccurate(String addrImg) throws InterruptedException {
    	AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");
        options.put("probability", "true");

        Thread.sleep(1000l);
        JSONObject res = client.basicAccurateGeneral(addrImg, options);
        try {
        	JSONArray ja = res.getJSONArray("words_result");
        	if(ja.length() == 0){
        		return "";
        	}
        	StringBuffer strBuf =  new StringBuffer();
        	for(int i=0;i<ja.length();i++){
    			  JSONObject job = ja.getJSONObject(i);   // 遍历 jsonarray 数组，把每一个对象转成 json 对象
    			  strBuf.append(job.get("words")); 
    		}
            return  strBuf.toString();
		} catch (JSONException e) {
			e.printStackTrace();
			return "";
		}
    }
    
    public static String sampleBasic(String addrImg) throws InterruptedException {
    	AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
	    options.put("detect_direction", "true");
	    options.put("detect_language", "true");
	    options.put("probability", "true");

	    Thread.sleep(1000l);
        JSONObject res = client.basicGeneral(addrImg, options);
        try {
        	JSONArray ja = res.getJSONArray("words_result");
        	if(ja.length() == 0){
        		return "";
        	}
        	StringBuffer strBuf =  new StringBuffer();
        	for(int i=0;i<ja.length();i++){
    			  JSONObject job = ja.getJSONObject(i);   // 遍历 jsonarray 数组，把每一个对象转成 json 对象
    			  strBuf.append(job.get("words")); 
    		}
            return  strBuf.toString();
		} catch (JSONException e) {
			e.printStackTrace();
			return "";
		}
    }
}

